﻿#include "AF_FlashAction.h"
#include "AF_Platform.h"

namespace AF
{
	FlashAction::FlashAction() :
		m_nTimes(0),
		m_ttLast(0),
		m_isHide(true),
		m_isHostVisible(false)
	{
	}

	FlashAction::~FlashAction()
	{
	}

	void FlashAction::SetEffect(Pointer pEffect)
	{
		AF_CHECK(pEffect != NULL);
		_MEMCPY(&m_Effect, (FlashEffect*)pEffect, sizeof(FlashEffect));		
	}
		
	void FlashAction::Flash(UINT nTimes, TimeTicks ttTime)
	{
		Flash(nTimes, ttTime >> 1, ttTime >> 1);
	}

	void FlashAction::Flash(UINT nTimes, TimeTicks ttHideTime, TimeTicks ttShowTime)
	{
		if(ttHideTime + ttShowTime == 0)
			return;

		FlashEffect _effect;
		_effect.nTimes = nTimes;
		_effect.ttHideTime = ttHideTime;
		_effect.ttShowTime = ttShowTime;
		SetEffect(&_effect);

		Play();
	}

	bool FlashAction::OnUpdate()
	{
		TimeTicks _ttNow = AF_pTHEPLATFORM->GetTimeTicks();

		if(m_isHide)
		{ // 隐
			if(_ttNow - m_ttLast >= m_Effect.ttHideTime)
			{ // 转为显
				if(m_Effect.ttShowTime > 0)
				{
// 					AF_NOTE("Turn Show\n");
					GetHostGraphics()->SetVisible(true);

					if(m_Effect.isFadeIn)
					{
						GetHostGraphics()->SetVisible(false);
						GetHostGraphics()->SetAlpha(0);
					}

					m_isHide = false;
				}
				m_ttLast = _ttNow;
			}
			else
			{
				if(m_Effect.isFadeOut && m_Effect.ttShowTime > 0)
				{ // 渐隐
					GetHostGraphics()->SetVisible(true);
					float fStep = (float)255 / (float)(m_Effect.ttShowTime);
					float fAlpha = 255 - (float)(_ttNow - m_ttLast) * fStep;
					GetHostGraphics()->SetAlpha((BYTE)fAlpha);
				}
			}
		}
		else
		{ // 显
			if(_ttNow - m_ttLast >= m_Effect.ttShowTime)
			{ // 转为隐
				// 次数判断
				if(m_Effect.nTimes > 0)
				{
					m_nTimes++;
					if(m_nTimes >= m_Effect.nTimes)
						return false;
				}

				if(m_Effect.ttHideTime > 0)
				{
// 					AF_NOTE("Turn Hide\n");
					GetHostGraphics()->SetVisible(false);

					if(m_Effect.isFadeOut)
					{
						GetHostGraphics()->SetVisible(true);
						GetHostGraphics()->SetAlpha(255);
					}

					m_isHide = true;
				}
				m_ttLast = _ttNow;
			}
			else
			{
				if(m_Effect.isFadeIn && m_Effect.ttHideTime > 0)
				{ // 渐显
					GetHostGraphics()->SetVisible(true);
					float fStep = (float)255 / (float)(m_Effect.ttHideTime);
					float fAlpha = (float)(_ttNow - m_ttLast) * fStep;
					GetHostGraphics()->SetAlpha((BYTE)fAlpha);
				}
			}

		}

		return true;
	}

	bool FlashAction::OnPlay(Bundle* pParam)
	{
		if(GetState() == AS_STOP)
		{
			m_nTimes = 0;
			m_isHostVisible = GetHostGraphics()->GetVisible();
	
			if(m_Effect.ttHideTime > 0)
			{
				GetHostGraphics()->SetVisible(false);
				if(m_Effect.isFadeIn)
				{
					GetHostGraphics()->SetVisible(true);
					GetHostGraphics()->SetAlpha(0);
				}

				m_isHide = true;
			}
			else
			{
				GetHostGraphics()->SetVisible(true);
				m_isHide = false;
			}
		}

		m_ttLast = AF_pTHEPLATFORM->GetTimeTicks();

		return true;
	}

	bool FlashAction::OnStop()
	{
		GetHostGraphics()->SetAlpha(255);
		GetHostGraphics()->SetVisible(m_isHostVisible);
		return true;
	}
}